package com.example;

import com.example.domain.Person;
import com.example.properties.AuthorSettings;
import com.example.selfproperties.*;
import com.example.service.HelloService;
import com.example.service.PersonService;
import com.example.task.AsyncTask1;
import com.example.task.AsyncTask2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.ModelAndView;

//@EnableConfigurationProperties({Prop3.class})
@RestController
@EnableAutoConfiguration
public class HelloController {

    @Value("${book.auther}")
    private  String bookAuther;

    @Value("${book.name}")
    private  String bookName;

    @Autowired
    private AuthorSettings authorSettings;

    /**
     * 和配置xml注解冲突，如果xml配置的bean也是 helloService
     */
    @Autowired
    private HelloService helloService;

//    @Resource
    @Autowired
    private PersonService personService;


    @Autowired
    private Prop1 prop1;//单个属性注解


    @Autowired
    private Prop2 prop2;//多属性注解，前缀，数组


    @Autowired
    private Prop3 prop3;//松散注解

    @Autowired
    private Prop4 prop4;//properties参数引用, 随机数

    @Autowired
    private Prop5 prop5;//自定义properties文件



    @Bean
    @ConfigurationProperties(prefix = "com.kfit.company")
    public Prop6 companyProperties3(){
        return new Prop6();
    }

    @Autowired
    private Prop6 prop6;//在App.java启动类中或者其它的类也是可以的，使用@Bean的方式进行注入。

    @Autowired
    private Prop7 prop7;//自定义结构

    @Autowired
    private AsyncTask1 task1;


    @Autowired
    private AsyncTask2 task2;


    @RequestMapping("/say")
    public  String sayHello(){
        return "--say hello : "+ bookAuther+","+bookName;
    }

    @RequestMapping("/say2")
    public  String sayHello2(){
        return "--say hello2 : "+ authorSettings.getName()+","+authorSettings.getAge();
    }

    @RequestMapping("/say3")
    @ResponseBody
    public String sayHello3(){
        return helloService.sayHello();
    }


    /*
    返回JSON之步骤：
    (1)编写一个实体类Demo
    (2)编写DemoController；
    (3)在DemoController加上@RestController和@RequestMapping注解；
    (4)测试*/
    @RequestMapping("/say4")
    public Person sayHello4(){
        Person person = new Person();
        person.setAge(11);
        person.setName("sp");
        return person;
    }

    /**
     * 全局异常 ControllerAdvice
     * GlobalDefaultExceptionHandler
     */
    @RequestMapping("/zero")
    public int zeroException(){
        return 100/0;
    }

    @RequestMapping("/helloy")
    public String hello() {
        System.out.println("--->prop1= "+prop1.getFilePathLocation());
        System.out.println("--->prop1= "+prop1.getFilePathLocation1());
        System.out.println("--->prop2= "+prop2.getName());
        System.out.println("--->prop2= "+prop2.getLocation());
        System.out.println("--->prop2= "+prop2.getMobile());
        System.out.println("--->prop2= "+prop2.getEmployCount());
        System.out.println("--->prop2= "+prop2.getEmploys().toString());
        System.out.println("--->prop3= "+prop3.getCompanyFullname ());
        System.out.println("--->prop3= "+prop3.getFirstName());
        System.out.println("--->prop3= "+prop3.getLogoPath());
        System.out.println("--->prop4= "+prop4.getDesc());
        System.out.println("--->prop4= "+prop4.getValue());
        System.out.println("--->prop4= "+prop4.getName());
        System.out.println("--->prop4= "+prop4.getBignumber());
        System.out.println("--->prop4= "+prop4.getTest1());
        System.out.println("--->prop4= "+prop4.getTest2());
        System.out.println("--->prop5= "+prop5.getLocation());
        System.out.println("--->prop5= "+prop5.getEmploys());
        System.out.println("--->prop5= "+prop5.getEmploys());
        System.out.println("--->prop6= "+prop6.getName());
        System.out.println("--->prop7= "+prop7.getEmployForzs().toString());
        System.out.println("--->prop7= "+prop7.getEmployForls().toString());
        System.out.println("--->prop7= "+prop7.toString());
        return "helloyyy";
    }

    //测试task1.
    @RequestMapping("/task1")
    public String task1() throws Exception{
        task1.doTaskOne();
        task1.doTaskTwo();
        task1.doTaskThree();
        return"task1";
    }

    @RequestMapping("/task2")
    public String task2() throws Exception{
        task2.doTaskOne();
        task2.doTaskTwo();
        task2.doTaskThree();
        return"task2";
    }


    @RequestMapping("/loginy")
    @ResponseBody
    public String login() {
        return "loginyyy";
    }

    /* 配置数据库
    大体步骤：
    (1)在application.properties中加入datasouce的配置
    (2)在pom.xml加入mysql的依赖。
    (3)获取DataSouce的Connection进行测试。1*/

    /* JPA练习
    (1)   创建实体类Demo,如果已经存在，可以忽略。
    (2)   创建jpa repository类操作持久化。
    (3)   创建service类。
    (4)   创建restful请求类。
    (5)   测试*/

    @RequestMapping("/save")
    public String jpaDemo(){
        Person d = new Person();
        d.setName("Angel");
        d.setAge(15);
        personService.save(d);//保存数据.
        return"ok.Demo2Controller.save";
    }



    @RequestMapping(value="/helloh",produces = "application/html; charset=utf-8")
    public ModelAndView helloh() {
        return new ModelAndView("helloh.html");
    }


    @RequestMapping(value="/loginh",produces = "application/html; charset=utf-8")
    public ModelAndView loginh() {
        return new ModelAndView("loginh.html");
    }
//
//
//    @RequestMapping(value="/ttjsp",produces = "application/html; charset=utf-8")
//    public ModelAndView ttjsp() {
//        return new ModelAndView("tt.jsp");
//    }
//
//
//
//
//    @Value("${application.hello:Hello Angel}")
//    private String hello;
//
//    @RequestMapping("/helloJsp")
//    public ModelAndView  helloJsp(ModelAndView modelAndView, ModelMap map){
//        System.out.println("HelloController.helloJsp().hello="+hello);
//        map.put("hello", "梅梅");
//        modelAndView.setViewName("/helloJsp");
//        return modelAndView;
//    }

}
